import defaultSettings from '../src/defaultSettings';
import {queryPermissions ,getNewRoutes}from './utils/utils';
import { getAuthority } from '@/utils/authority';
import {message} from 'antd';
export const dva = {
  config: {
    onError(err) {
      err.preventDefault();
    },
  },
};

let authRoutes = {};
let index = 1;
let index2= 0;
function getchaildPath(routes) {
  if(routes[index2]){
    if(routes[index2].hideInMenu){
      index2 = index2+1;
      return getchaildPath(routes);
    }else{
      return routes[index2].path;
    }
  }else{
    return '/exception/500'
  }

}
function getMainPath(routes) {

  if(routes && routes[index]){

    /*if(routes[index].hideInMenu){//说明父菜单是被隐藏的菜单
      index = index+1;
      return getMainPath(routes);
    }else{*/
    if(routes[index].routes){//说明有子菜单
      if(routes[index].routes.length>0){
        const routes2 = routes[index].routes ;
        return getchaildPath(routes2);
      }else{
        message.warning("筛选主菜单时发现父菜单无任何子菜单有权限!");
        return '/exception/403'
      }
    }else{//说明没有子菜单
      return routes[index].path;
    }

  }else {
    message.warning("筛选主菜单时发现无任何权限!");
    return '/exception/403'
  }
}

/*function ergodicRoutes(routes, authKey, authRoutes) {//routes 是路由 、authKey 是/form/advanced-form  authority ["admin","user"]

  routes.forEach(element => {
    // 如果 element.authority 菜单里的权限是没配置的 那就给个 []
    if (!element.authority) element.authority = [];
    if (element.path === authKey) {//如果路由里的 path 等于 /form/advanced-form
      Object.assign(element.authority, authRoutes.authority || []); //Object.assign(a,b); 把b的数据塞到a里
      if(authRoutes.seq) element.seq = authRoutes.seq
      if (!element.hideInMenu) element.hideInMenu = false;
      element.hideInMenu = authRoutes.hideInMenu
    } else if (element.routes) {//如果有子集就再走一遍
      ergodicRoutes(element.routes, authKey, authRoutes);
    }
    return element;
  });

}*/

function ergodicRoutes(routes,authRoutes) {//routes 是路由 、authKey 是/form/advanced-form  authority ["admin","user"]
  routes.forEach(element => {
    if(element.routes && element.routes.length>0){
      ergodicRoutes(element.routes, authRoutes);
    }else if(element.permission_id === authRoutes.permission_id){
      element.component= authRoutes.component;
      element.parameter = authRoutes.parameter ? authRoutes.parameter :undefined;
      //console.log("element--",element);
      if(element.is_left_menu === '4'){
        element.path =   authRoutes.path && authRoutes.path!=''? authRoutes.path :element.path;
      }
    }
    return element;
  });

}
function getAuthRoute(routes) {
  let autoRoute = [];
  routes.forEach(element => {
    if(element.routes && element.routes.length>0){
      autoRoute.push(...getAuthRoute(element.routes));
    }else if(element.component && element.permission_id){
      autoRoute.push(element);
    }
  })
  return autoRoute
}
function noAuthErgodicRoutes(routes, authKey, authority) {
  routes.forEach(element => {
    if (element.path === authKey) {
      if (!element.authority) {
        // eslint-disable-next-line no-param-reassign
        element.authority = [];
      }
      // eslint-disable-next-line compat/compat
      Object.assign(element.authority, authority || []);
    } else if (element.routes) {
      noAuthErgodicRoutes(element.routes, authKey, authority);
    }
    return element;
  });
}
/*function sortRoute(routes) {
  routes.sort(function(a,b){
    if(a.routes)sortRoute(a.routes);
    if(a.seq<b.seq){
      return -1;
    }else if(a.seq>b.seq){
      return 1;
    }
    return 0;
  });

}*/
function delRoute(routes) {//删除无关路由
  //debugger
  const delList = [];
  for(let i=0;i<routes.length;i++){
    if(routes[i].path != '/' && !routes[i].redirect){
      if(routes[i].routes && routes[i].routes.length>0)delRoute(routes[i].routes);

      if((!routes[i].routes || routes[i].routes.length===0) && !routes[i].component) {
        //delList.push(i);
        routes.splice(i,1)
        i=i-1;
      }else {
        console.log("不用删除的数组");
        console.log(routes[i]);
      }
    }
  }
  /*console.log("==========故事开始===============");
  console.log(routes);
  console.log(delList);
  console.log("==========故事结束===============");
  for(let b = 0; b<delList.length;b++){
    //
    routes.splice(delList[b],1);
  }*/

}
export function patchRoutes(routes) {

  if(defaultSettings.Authority_control){
    let reMenu = getAuthRoute(routes[1].routes);
    const role_permissions =getNewRoutes(queryPermissions());

    for(let i = 0; i<reMenu.length;i++){
      ergodicRoutes(role_permissions,reMenu[i]);
    }
    role_permissions.push(routes[1].routes[routes[1].routes.length-1])
    //console.log("====0=============",role_permissions);
    routes[1].routes=role_permissions;
    delRoute(routes[1].routes);

    const auth = getAuthority();
    if(auth &&  auth !=  'notLogin'){
      let redirect =getMainPath(routes[1].routes);
      routes[1].routes[0] = { //定义首页跳转到哪
        path: '/',
        redirect:redirect,
        authority : ['allUsers'],
        exact:true,
      }
    }

  }else{
    Object.keys(authRoutes).map(authKey =>
      noAuthErgodicRoutes(routes, authKey, authRoutes[authKey].authority)
    );
    window.g_routes = routes;
  }

  /*routes[0].routes.push({
    "component": "404",
    "authority": [
      "allUsers"
    ]
  })*/
  console.log(JSON.stringify(routes));
  window.g_routes = routes;
}

export function render(oldRender) {
  let ret ={};
  const role_permissions = queryPermissions();
  if(role_permissions && role_permissions.length >0){
    for(let i = 0 ;i < role_permissions.length ; i++){
      ret[role_permissions[i].route]={
        seq:role_permissions[i].seq,
        authority:role_permissions[i].roles,
        hideInMenu:!role_permissions[i].left_menu,
        permission_id :role_permissions[i].permission_id,
      }
    }
  }
  authRoutes = ret;
  oldRender();
}

